package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhidao.dao.CollectionsMapper;
import com.example.zhidao.model.Spage.ShowPage;
import com.example.zhidao.model.po.Blog;
import com.example.zhidao.model.po.Collections;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.CollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/collections")
public class CollectionsController {//3

    @Autowired
    CollectionsService collectionsService;

    @Resource
    CollectionsMapper collectionsMapper;


//    获取该用户是否收藏改博客
//    为了提升性能，将两个请求合为一个请求，这个接口详情请去GoodsService
//    @GetMapping("isCollectionsBlog")
//    public ResponseData isCollectionsBlog(String uid,String bid){
//        QueryWrapper<Collections> qw=new QueryWrapper<>();
//        qw.eq("uid",uid);
//        qw.eq("bid",bid);
//        Collections collections=collectionsService.getOne(qw);
//
//        if(collections==null){
//            return new ResponseData(0,false,"未收藏",200);
//        }else{
//            return new ResponseData(1,false,"已收藏",200);
//        }
//    }

    //根据用户id获得，该用户的收藏博客
    @GetMapping
    public ResponseData selCollectionsByUid(String uid, @RequestParam(defaultValue = "1") Integer page ,
                                            @RequestParam(defaultValue = "10") Integer pageSize ){
        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Collections> qw=new QueryWrapper<>();
        qw.eq("uid",uid);
        showPage.setSize(collectionsService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(collectionsMapper.selConnectionBlogByUid(uid,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取收藏博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }

    }


//    收藏加一
    @PostMapping("/add")
    public ResponseData addCollection(Collections collections){
        QueryWrapper<Collections> qw=new QueryWrapper<>();
        qw.eq("uid",collections.getUid());
        qw.eq("bid",collections.getBid());

        Collections c=collectionsService.getOne(qw);

        if(c!=null){
            return new ResponseData(null,false,"已经收藏",202);
        }else{
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
            collections.setId(uuid);
            boolean i=collectionsService.addColl(collections);

            if(i){
                return new ResponseData(1,true,"收藏成功",200);
            }else{
                return new ResponseData(0,false,"收藏失败",201);

            }
        }
    }

    //    收藏加一
    @DeleteMapping("/del")
    public ResponseData delCollection(Collections collections){
        QueryWrapper<Collections> qw=new QueryWrapper<>();
        qw.eq("uid",collections.getUid());
        qw.eq("bid",collections.getBid());

        Collections c=collectionsService.getOne(qw);

        if(c!=null){
            boolean i=collectionsService.delColl(c);

            if(i){
                return new ResponseData(1,true,"取消收藏成功",200);
            }else{
                return new ResponseData(0,false,"取消收藏失败",201);

            }
        }else{
            return new ResponseData(null,false,"还未收藏",202);

        }
    }
}
